package com.tjlg.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjlg.auto.JSONAuthentication;
import com.tjlg.entity.WaLx;
import com.tjlg.entity.WaUser;
import com.tjlg.mapper.WaLxMapper;
import com.tjlg.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tjlg
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/wa-lx")
public class WaLxController extends JSONAuthentication {
    @Autowired
    WaLxMapper waLxMapper;
    @ApiOperation(value = "添加路线")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody WaLx waLx) throws  Exception{
      QueryWrapper wrapper=new QueryWrapper();
      wrapper.eq("lx_name",waLx.getLxName());
      if(waLxMapper.selectOne(wrapper)!=null){
          Result error = Result.error().message("路线名字不可重复");
          this.WriteJSON(request, response, error);
      }else{
        if(waLxMapper.insert(waLx)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
      }
    }
    @ApiOperation(value = "删除路线")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=waLxMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改路线")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody WaLx waLx) throws Exception{
        WaLx waLx1 = waLxMapper.selectById(waLx.getId());
        waLx1.setStart(waLx.getStart());
        waLx1.setEnd(waLx.getEnd());
        waLx1.setLxName(waLx.getLxName());
        if(waLxMapper.updateById(waLx1)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有路线")
    @PostMapping("/findAllLx")
    public void findAllLx(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<WaLx> shUserPage=new Page<>(current,size);
        Page<WaLx> shUserPages=waLxMapper.selectPage(shUserPage,null);
        List<WaLx> records = shUserPages.getRecords();
        int total=waLxMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("lx",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找路线通过路线名")
    @PostMapping("/findAllLxByName")
    public void findAllLxByName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("name") String name)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("lx_name",name);
        Page<WaLx> shUserPage=new Page<>(current,size);
        Page<WaLx> shUserPages=waLxMapper.selectPage(shUserPage,wrapper);
        List<WaLx> records = shUserPages.getRecords();
        int total=waLxMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("lx",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据路线id查询路线")
    @PostMapping("/findAllLxByid")
    public void findAllLxByid(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
       WaLx waLx=waLxMapper.selectById(id);
        if(waLx!=null){
            Result ok = Result.ok().data("lx",waLx);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

